﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;

public partial class reports_displayGroceryRescueReport : System.Web.UI.Page
{
    public DataTable source { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ReportDataSource source = new ReportDataSource("dataSet", (DataTable)(LoadData(DateTime.Now.AddDays(-5), DateTime.Now).GroceryRescue));
            reportViewer.LocalReport.DataSources.Clear();
            reportViewer.LocalReport.DataSources.Add(source);
            reportViewer.DataBind();
            reportViewer.LocalReport.Refresh();
            reportViewer.LocalReport.SetParameters(new ReportParameter("startDate", "12/1/12"));
            reportViewer.LocalReport.SetParameters(new ReportParameter("endDate", "12/7/12"));

        }    
    }

    public ReportsDataSet TestDataSet()
    {
        ReportsDataSet ds = new ReportsDataSet();
        for (int i = 0; i < 10; i++)
        {
            string storeName = "Store " + i.ToString();
            string storeAddress = "Address " + i.ToString();
            string storeID = "000" + i.ToString();
            string bakeryWeight = (i * 22).ToString();
            string dairyWeight = (i * 2).ToString();
            string meatWeight = (i * 100).ToString();
            string produceWeight = (i * 43).ToString();
            string otherWeight = (i * 24).ToString();
            ds.GroceryRescue.AddGroceryRescueRow(storeAddress, storeAddress, storeID, bakeryWeight, dairyWeight, meatWeight, produceWeight, otherWeight);
        }

        return ds;
    }

    private ReportsDataSet LoadData(DateTime startDate, DateTime endDate)
    {
        List<FoodSource> data;
        ReportsDataSet ds = new ReportsDataSet();
        using (CCSEntities db = new CCSEntities())
        {
            data = (from f in db.FoodSources
                    where f.FoodSourceType.FoodSourceType1 == "Grocery Rescue"
                    orderby f.Source
                    select f).ToList();

           
            foreach (FoodSource row in data)
            {
                string storeName = row.Source;
                Address add = row.Addresses.First();
                string storeAddress = string.Format("{0} {1} {2}", add.StreetAddress1, add.StreetAddress2, add.City.CityName);
                string storeID = row.StoreID;


                var foodIn = from f in row.FoodIns
                             where f.TimeStamp >= startDate && f.TimeStamp <= endDate
                             select f;
                string bakeryWeight = (from f in foodIn
                                       where f.FoodCategory.CategoryType == "Bakery"
                                       select f.Weight).Sum().ToString();
                string dairyWeight = (from f in foodIn
                                      where f.FoodCategory.CategoryType == "Dairy"
                                      select f.Weight).Sum().ToString();
                string meatWeight = (from f in row.FoodIns
                                     where f.FoodCategory.CategoryType == "Meat"
                                     select f.Weight).Sum().ToString();
                string produceWeight = (from f in foodIn
                                        where f.FoodCategory.CategoryType == "Produce"
                                        select f.Weight).Sum().ToString();
                string otherWeight = (from f in foodIn
                                      where f.FoodCategory.CategoryType != "Produce" &&
                                      f.FoodCategory.CategoryType != "Meat" &&
                                      f.FoodCategory.CategoryType != "Dairy" &&
                                      f.FoodCategory.CategoryType != "Bakery"
                                      select f.Weight).Sum().ToString();
                ds.GroceryRescue.AddGroceryRescueRow(storeAddress, storeName, storeID, bakeryWeight, dairyWeight, meatWeight, produceWeight, otherWeight);

            }
        }
        return ds;
    }
}